package com.xkstream.client.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xkstream.client.R;
import com.xkstream.client.api.API;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.entity.Area;
import com.xkstream.client.presenter.AreaCodeContract;
import com.xkstream.client.presenter.AreaCodePresenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/6/12.
 */

public class AreaCodeActivity extends BaseActivity<AreaCodeContract.Presenter> implements
        AreaCodeContract.View, AdapterView.OnItemClickListener {

    private List<Area> areas = new ArrayList<>();

    private Context context;

    private AreaCodeAdapter adapter;

    @BindView(R.id.lvAreaCode)
    ListView lvAreaCode;

    @OnClick(R.id.ivBack)
    public void onClick(View v) {
        finish();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_areacode);
        ButterKnife.bind(this);
        presenter = new AreaCodePresenter(this);
        context = this;
        adapter = new AreaCodeAdapter();
        lvAreaCode.setAdapter(adapter);
        lvAreaCode.setOnItemClickListener(this);
        loadAreaCode();
    }

    private void loadAreaCode() {
        presenter.getAreaList();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Area area = areas.get(position);
        Intent intent = new Intent();
        intent.putExtra("area", area);
        setResult(-1, intent);
        finish();
    }

    @Override
    public void setPresenter(AreaCodeContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showAreaList(BaseData<List<Area>> data) {
        if (data.getCode() == 1) {
            areas = data.getData();
            adapter.notifyDataSetChanged();
        }

    }

    private class AreaCodeAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return areas.size();
        }

        @Override
        public Object getItem(int position) {
            return areas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.layout_game_item, null);
                holder = new ViewHolder();
                holder.tvGame = (TextView) convertView.findViewById(R.id.tvGame);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String language = getResources().getConfiguration().locale.getCountry();
            if (language.equals("CN")) {
                holder.tvGame.setText(areas.get(position).getChain_name());
            } else if (language.equals("UK") || language.equals("US")) {
                holder.tvGame.setText(areas.get(position).getEnglish_name());
            } else {
                holder.tvGame.setText(areas.get(position).getChain_name());
            }

            return convertView;
        }

        private class ViewHolder {
            TextView tvGame;
        }
    }
}
